// pages/discoverydetail/discoverydetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _id: "",

    me: {},
    imgheight: 0,
    cbheight: {},
    kbheight: 0,
    input: false,
    kbpadding: "env(safe-area-inset-bottom)",
    //删除对话框
    dialogShow: false,
    buttons1: [{
      text: '取消'
    }, {
      text: '删除'
    }],
    //半屏菜单
    show: false,
    //
    follow: "follow",
    //提交评论
    commentValue: "",
    value: "",
    allComment: [],
    //删除评论的actionsheet
    commentDeleteTemp: "",
    showActionsheet: false,
    groups: [
      {
        text: '删除',
        type: 'warn',
        value: 1
      }
    ]
  },

  //半屏
  open: function () {
    this.setData({
      show: true
    })
    var that = this
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    this.getAllComment()
    
  },
//获取所有评论
getAllComment: function() {
  var that = this
  wx.cloud.callFunction({
    name: "getAllComment",
    data: {
      artId: this.data._id
    }
  }).then(res => {
    wx.hideLoading()
    console.log(res.result)
    that.setData({
      allComment: res.result
    })
  })
},
  //关注其他用户
  follow: function () {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      follow: ""
    })
    wx.cloud.callFunction({
      name: 'addfollow',
      data: {
        follow: this.data.userinfo._openid
      }
    }).then(res => {
      console.log(res)
      if (res.result == true) {
        this.setData({
          unfollow: false
        })
        wx.showToast({
          title: '关注成功',
          icon: 'done',
          duration: 2000
        })
      }
      wx.hideLoading()
    }).catch(e => {
      this.setData({
        follow: "follow"
      })
      wx.hideLoading()
    })
  },

  //删除对话框
  openConfirm: function () {
    this.setData({
      dialogShow: true
    })
  },
  tapDialogButton(e) {
    var that = this
    this.setData({
      dialogShow: false
    })
    if (e.detail.index == 1) {
      wx.showLoading({
        title: '正在删除',
        mask: true
      })
      wx.cloud.callFunction({
        name: "deletePost",
        data: {
          artId: that.data._id
        }
      }).then(res => {
        wx.showToast({
          title: '已删除',
          duration: 2000,
          mask: true
        })
        setTimeout(function () {
          that.back()
        }, 2000)
      })
    }
  },

  //返回
  back: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  //发送评论
  commentAct: function (e) {
    console.log("commentAct")
    console.log(e.detail.height)
    this.setData({
      kbheight: e.detail.height,
      commentDis: "commentDis",
      kbpadding: "0rpx",
      show: false
    })
  },
  commentDis: function () {
    console.log("commentDis")
    this.setData({
      input: false,
      kbheight: 0,
      kbpadding: "env(safe-area-inset-bottom)",
    })
  },
  getTextAreaV: function (e) {
    console.log(e.detail.value)
    this.setData({
      commentValue: e.detail.value
    })
  },
  Csubmit: function (e) {
    if (this.data.commentValue != "") {
      wx.showLoading({
        title: '正在发送',
        mask: true
      })
      var that = this
      var time = new Date().getTime()
      wx.cloud.callFunction({
        name: "submitComment",
        data: {
          maintext: that.data.commentValue,
          timestamp: time,
          artId: that.data._id
        }
      }).then(res => {
        console.log(res)
        that.setData({
          value: ""
        })
        wx.showToast({
          title: '发送成功',
        })
      })
    }
  },

  //一键三连

  heart: function () {
    wx.vibrateShort({
      type: 'medium',
    })
    console.log("heart")
    var that = this
    if (this.data.discoveryDetail.userCtrl.userHeart) {
      //移除
      this.setData({
        'discoveryDetail.userCtrl.userHeart': 0,
        'discoveryDetail.artCtrl.artHeart': this.data.discoveryDetail.artCtrl.artHeart - 1
      })

      wx.cloud.callFunction({
        name: "deleteHeart",
        data: {
          artId: that.data._id
        }
      })
    } else {
      this.setData({
        'discoveryDetail.userCtrl.userHeart': 1,
        'discoveryDetail.artCtrl.artHeart': this.data.discoveryDetail.artCtrl.artHeart + 1
      })
      wx.cloud.callFunction({
        name: "addHeart",
        data: {
          artId: that.data._id
        }
      })
    }
  },

  star: function () {
    wx.vibrateShort({
      type: 'medium',
    })
    console.log("star")
    var that = this
    if (this.data.discoveryDetail.userCtrl.userStar) {
      //移除
      this.setData({
        'discoveryDetail.userCtrl.userStar': 0,
        'discoveryDetail.artCtrl.artStar': this.data.discoveryDetail.artCtrl.artStar - 1
      })

      wx.cloud.callFunction({
        name: "deleteStar",
        data: {
          artId: that.data._id
        }
      })
    } else {
      this.setData({
        'discoveryDetail.userCtrl.userStar': 1,
        'discoveryDetail.artCtrl.artStar': this.data.discoveryDetail.artCtrl.artStar + 1
      })
      wx.cloud.callFunction({
        name: "addStar",
        data: {
          artId: that.data._id
        }
      })
    }
  },

  //删除评论
  close: function () {
    this.setData({
      showActionsheet: false
    })
  },
  btnClick(e) {
    var that = this
    if(e.detail.value) {
      console.log("delete "+this.data.commentDeleteTemp)
      wx.showLoading({
        title: '删除中',
        mask: true,
      })
      //云函数 删除
      wx.cloud.callFunction({
        name: "deleteComment",
        data:{
          commentId: that.data.commentDeleteTemp
        }
      }).then(res => {
        console.log(res)
        this.getAllComment()
        wx.showToast({
          title: '已删除',
          icon: "success",
          duration: 2000
        })
      })
    }
    this.setData({
      commentDeleteTemp: ""
    })
    this.close()
  },
  cancelDelete(e){
    console.log("bushanchule")
    this.setData({
      commentDeleteTemp: ""
    })
  },

  deleteComment: function (e) {
    console.log(e.currentTarget.dataset)
    if (e.currentTarget.dataset.isme) {
      this.setData({
        showActionsheet: true,
        commentDeleteTemp: e.currentTarget.dataset.commentid
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    console.log(e)
    wx.showLoading({
      title: '加载中',
    })
    //评论下面显示的头像
    this.setData({
      me: getApp().globalData.userInfo,
      _id: e.art
    })
    //文章数据
    wx.cloud.callFunction({
      name: "getPostDetail",
      data: {
        _id: e.art
      }
    }).then(res => {
      console.log(res.result)
      console.log(res.result.images[0])
      this.setData({
        discoveryDetail: res.result,
      })
      wx.cloud.downloadFile({
        fileID: res.result.images[0]
      }).then(res => {
        console.log(res.tempFilePath)
        wx.getImageInfo({
          src: res.tempFilePath,
          success(res) {
            console.log(res)
            that.setData({
              imgheight: 750 * res.height / res.width + 1
            })
          },
          fail(err) {
            console.log(err)
          }
        })
      }).catch(error => {
        console.log(error)
      })
      wx.hideLoading()
    })
    wx.getSystemInfo({
      success: (result) => {
        console.log(result.screenHeight - result.safeArea.bottom)
        that.setData({
          cbheight: result.screenHeight - result.safeArea.bottom
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})